﻿using AFanDa.BLL.BLLService;
using AFanDa.Dto.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThoughtWorks.QRCode.Codec;

namespace AFanDa.Admin.Controllers
{
    public class TableController : Controller
    {
        // GET: Table


        TableTypeService _tableTypeService = null;
        ///
        public TableController()
        {
            _tableTypeService = new TableTypeService();
        }

        /// <summary>
        /// 添加餐桌类型 （彭思期）
        /// </summary>
        /// <param name="tableType"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddTableType(Dto.Request.tableType tableType)
        {
            var result = _tableTypeService.AddTableType(tableType);

            return Json(result);
        }

        /// <summary>
        /// 获取餐桌类型 （无条件） （彭思期）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetTableType(bool isAsc = true, string key = "")
        {
            key = key ?? "";
            var result = _tableTypeService.getTableType(new Dto.Request.tableType()
            {
                key = key,
                isAsc = isAsc
            });
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加餐桌 （彭思期）
        /// </summary>
        /// <param name="tableRequestDto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult addtables(Dto.Request.tableRequestDto tableRequestDto)
        {

            string id = Common.Config.GUID();
            string str = Add(id);

            tableRequestDto.ErWeiMa = str;
            tableRequestDto.id = id;


            var result = _tableTypeService.AddTable(tableRequestDto);



            return Json(result);

        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public string Add(string tid)
        {
            string nr = "http://localhost:8080/#/?tid=" + tid;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 8;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            System.Drawing.Image image = qrCodeEncoder.Encode(nr);
            string filename = Common.Config.GUID() + ".jpg";
            string path = "/Source/TableImg/" + filename;
            string filepath = Server.MapPath(path);
            System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);

            fs.Close();
            image.Dispose();
            return path;

        }

        /// <summary>
        ///  获取桌子
        /// </summary>
        /// <param name="number"></param>
        /// <param name="type"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult getTable(string number, string type, int status)
        {
            var result = _tableTypeService.GetTable(number, type, status);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据id 获取桌子
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetTableById(string id)
        {
            var result = _tableTypeService.GetTableById(id);
            return Json(result,JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 桌子类型详情页面 （肖永琪负责）
        /// </summary>
        /// <returns></returns>
        public ActionResult TableTypeInfo()
        {
            return View();
        }

        /// <summary>
        /// 修改桌子类型页面 （肖永琪负责）
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateTableHtml()
        {
            return View();
        }

        /// <summary>
        /// 桌子类型详情 （肖永琪负责）
        /// </summary>
        /// <returns></returns>
        public ActionResult TableDetialsHtml()
        {
            return View();
        }

        /// <summary>
        /// 修改餐桌类型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult UpdateTableType(string id, string name)
        {
            var reuslt = _tableTypeService.UpdateTableType(new Dto.Request.tableType()
            {
                id = id,
                name = name
            });
            return Json(reuslt);
        }

        /// <summary>
        /// 编辑桌子页面
        /// </summary>
        /// <returns></returns>
        public ActionResult EditTableHtml()
        {
            return View();
        }

        /// <summary>
        /// 修改桌子
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditTable(Dto.Request.tableRequestDto dto)
        {
            var result = _tableTypeService.EditTable(dto);

            return Json(result);
        }

    }
}